<DOCTYPE html>
  <html lang="en">

  <head charset="UTF-8">
    <title></title>
  </head>

  <body>
    <script>
      //append insert get indexOf update removeAt remove
      function DoubleLinkList() {
        let head = null;
        let tail = null
        let length = 0
        function Node(data) {
          let data = data;
          let next = null;
          let prev = null
        }

        DoubleLinkList.prototype.append = data => {
          let node = new Node(data)
          if (this.length == 0) {
            this.head = node;
            this.tail = node
          } else {
            node = this.tail.prev.next;
            current.next = node;
            this.tail = node
          }
          this.length++;
        }

        DoubleLinkList.prototype.insert = (position, data) => {
          if (position < 0 || position > this.length) {
            return false
          }
          if (this.length == 0 || position == this.length) {
            this.apped(data);
          } else {
            let node = new Node(data);
            if (position < Math.floor(this.length / 2)) {
              let current = this.head;
              let index = -1;
              while (++index < position) {
                current = current.next
              }
              node.next = current.next
              current.next.prev = node
              current.next = node
              node.prev = current
            } else {
              let current = this.tail;
              let index = this.length;
              while (--index > position) {
                current = current.prev
              }
              node.prev = current.prev
              current.prev.next = node
              current.prev = node
              node.next = current
            }
            this.length++;
          }
        }

        DoubleLinkList.prototype.get = position => {
          if (position < 0 || position > this.length || this.length == 0) {
            return null
          } else {
            if (position < Math.floor(this.length / 2)) {
              let current = this.head;
              let index = -1
              while (++index < position) {
                current = current.next
              }
            } else {
              let current = this.tail;
              let index = this.length
              while (--index > position) {
                current = current.prev
              }
            }
            return current.data
          }
        }

        DoubleLinkList.prototype.indexOf = data => {
          if (this.length == 0) {
            return -1
          } else {
            if (position < Math.floor(this.length / 2)) {
              let current = this.head;
              let index = 0
              while (current.next != null) {
                if (current.data == data) {
                  return index
                }
                index++;
                current = current.next;
              }
            } else {
              let current = this.tail;
              let index = this.length - 1
              while (current.prev != null) {
                if (current.data == data) {
                  return index
                }
                index--;
                current = current.prev;
              }
            }
            return -1
          }

          DoubleLinkList.prototype.update = (position, data) => {
            if (position < 0 || position > this.length || this.length == 0) {
              return false
            } else {
              if (position < Math.floor(this.length / 2)) {
                let current = this.head;
                let index = -1
                while (++index < position) {
                  current = current.next
                }
                current.data = data
              } else {
                let current = this.tail;
                let index = this.length
                while (--index > position) {
                  current = current.prev
                }
                current.data = data
              }
            }
          }

          DoubleLinkList.prototype.removeAt = position => {
            if (position < 0 || position > this.length || this.length == 0) {
              return false
            } else {
              let current = this.head;
              if (position == 0) {
                this.head = current.next
              } else {
                if (position < Math.floor(this.length / 2)) {
                  let index = 0
                  while (++index < position) {
                    current = current.next
                  }
                  current.next = current.next.next
                } else {
                  let index = this.length - 1
                  while (--index > position) {
                    current = current.prev
                  }
                  current.prev = current.prev.prev
                }
                this.length--;
              }
            }
          }
        }

        DoubleLinkList.prototype.remove = () => {
          if (this.length == 0) {
            return false
          }
          if (this.length == 1) {
            this.head = null;
            this.tail = null;
          } else {
            let current = this.tail;
            let index = this.length - 2
            while (--index > this.length) {
              current = current.prev
            }
            current.next.prev = null
            current.next = null
          }
          this.length--;
        }

        DoubleLinkList.prototype.toString = () => {
          if (this.length == 0) {
            return null
          }
          let result = '';
          let current = this.head
          do {
            result += current.data;
            current = current.next
          } while (current.next == null);
          return result
        }
      }
    </script>
  </body>

  </html>